package com.coded2.infra.interceptor.transactional;

import java.io.Serializable;

import javax.inject.Inject;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;
import javax.persistence.EntityManager;

@Interceptor @Transaction
public class TransactionInterceptor implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -6631716192349726085L;
	@Inject
	private EntityManager em;

	@AroundInvoke
	public Object aroundInvoke(InvocationContext ic) throws Exception {
		Object result = null;
		em.getTransaction().begin();
		result = ic.proceed();
		em.getTransaction().commit();
		return result;
	}

}
